package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐的分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    @Cacheable(value = "setmeal",key = "#pageNum+'_'+#pageSize+'_'+#name")
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //创建查询条件对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        wrapper.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        //创建分页对象
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //使用条件对象和分页对象，查询套餐的分页数据
        page = setmealMapper.selectPage(page, wrapper);
        //取出每一个套餐，使用套餐中的分类id查询分类对象
        List<Setmeal> list = page.getRecords();
        if (CollectionUtil.isNotEmpty(list)) {
//            for (Setmeal setmeal : list) {
//                //使用套餐的分类id查询对象
//                Category category = categoryMapper.selectById(setmeal.getCategoryId());
//                //把分类名称存入套餐对象中
//                setmeal.setCategoryName(setmeal.getName());
//                //使用套餐的id作为条件，查询套餐菜品中间表，获取当前套餐的所有菜品信息
//                LambdaQueryWrapper<SetmealDish> setmealDish = new LambdaQueryWrapper<>();
//                setmealDish.eq(SetmealDish::getSetmealId, setmeal.getId());
//                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDish);
//                //把查询结果存入套餐
//                setmeal.setSetmealDishes(setmealDishes);
//            }

 list.parallelStream().forEach(setmeal -> {
//     使用套餐的分类id查询对象
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                //把分类名称存入套餐对象中
                setmeal.setCategoryName(setmeal.getName());
                //使用套餐的id作为条件，查询套餐菜品中间表，获取当前套餐的所有菜品信息
                LambdaQueryWrapper<SetmealDish> setmealDish = new LambdaQueryWrapper<>();
                setmealDish.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDish);
                //把查询结果存入套餐
                setmeal.setSetmealDishes(setmealDishes);
 });

        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //保存套餐对象
        setmealMapper.insert(setmeal);
        //取出套餐中选择的菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //通过遍历为每个菜品设置套餐的id
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                //把菜品存入套餐菜品关联表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }


    @Override
    public void deleteById(Long[] ids) {
        //1.确认被删除的套餐全部处于在售状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0) {
            throw new CustomException("当前套餐有处于在售状态，不允许删除");
        }
        //2.执行删除
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getDishId, id);
            setmealDishMapper.delete(qw);
            //删除套餐表
            setmealMapper.deleteById(id);
        }
    }
    @Override
    public Setmeal updateFindById(Long setmealId) {
        //根据套餐id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(setmealId);
        //再根据套餐id查询菜品列表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //将菜品设置到套餐信息
        setmeal.setSetmealDishes(setmealDishes);
        //返回套餐信息
        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //直接更新套餐信息
        setmealMapper.updateById(setmeal);
        //根据套餐id删除菜品列表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //遍历新套餐列表
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //设置套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存数据库
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public List<Setmeal> findByCategoryAndStatus(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, categoryId);
        lqw.eq(Setmeal::getStatus, status);
        List<Setmeal> setmeals = setmealMapper.selectList(lqw);
        return setmeals;
    }

    @Override
    public void updateStatus(Long[] ids, Integer status) {
        for (Long id : ids) {
            //根据套餐id查询套餐信息
            Setmeal setmeal = setmealMapper.selectById(id);
            //将状态设置到套餐信息
            setmeal.setStatus(status);
            //更新
            setmealMapper.updateById(setmeal);
        }

    }

    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<Dish> finddish(Long id) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lqw);
        List<Dish> dishes = new ArrayList<>();

        for (SetmealDish dish : setmealDishes) {
            Dish dish1 = dishMapper.selectById(dish.getDishId());
            dishes.add(dish1);
        }
        return dishes;
    }
}
